package patterns.statePattern;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {

	public static void main(String[] args) throws Exception {
 		 State redLight = new RedLight();
		 State greenLight = new GreenLight();
		 State yellowLight = new YellowLight();
		 Map<State, Integer> map1 = new HashMap<>();
		 Map<State, Integer> map2 = new HashMap<>();
		 Map<State, Integer> map3 = new HashMap<>();
		 map1.put(redLight, 2);
		 map2.put(greenLight, 10);
		 map3.put(yellowLight, 1);
		 Map<State, Integer>[] stateTimeMapper = new HashMap[3];
		 stateTimeMapper[0] = map1;
		 stateTimeMapper[1] = map2;
		 stateTimeMapper[2] = map3;
		 Map<String, State> walkmanPassingState = new HashMap<>();
		 walkmanPassingState.put("walkmanPass", redLight);
		 TrafficLights trafficLights = TrafficLightFactory.newInstance("成都市天府四街",
		 redLight, stateTimeMapper, walkmanPassingState);
		 Walkman tom = new Young("tom", "man", 24, HumanHealthStatus.HEALTHY);
		 Vehicle car = new Car("susan");
		 Thread.sleep(5000l);
		 tom.meetTrafficLights(trafficLights);
		 car.meetTrafficLights(trafficLights);
		 Walkman[] walkmans = new Walkman[100];
		 Vehicle[] vehicles = new Vehicle[100];
		 for (int i = 0; i < 100; i++) {
		 walkmans[i] = new Young("young" + i, "man", 25,
		 HumanHealthStatus.HEALTHY);
		 vehicles[i] = new Car("driverName" + i);
		 }
		while (true) {
			System.out.println("输入：");
			Scanner input = new Scanner(System.in);
			String command = input.nextLine();
			// "aw3t50" "dw3t60"
			String type = command.substring(0, 2);
			int start = Integer.valueOf(command.substring(2,command.indexOf("t")));
			int end = Integer.valueOf(command.substring(command.indexOf("t")+1));
			if("aw".equals(type)){
				if(end == start){
					walkmans[end].meetTrafficLights(trafficLights);
				}else {
					for(int i=start;i<=end;i++){
						walkmans[i].meetTrafficLights(trafficLights);
					}
				}
			}else if("dw".equals(type)) {
				if(end == start){
					walkmans[end].renouncePassTrafficLights();
				}else {
					for(int i=start;i<=end;i++){
						walkmans[i].renouncePassTrafficLights();
					}
				}
			}else if ("av".equals(type)) {
				if(end == start){
					vehicles[end].meetTrafficLights(trafficLights);
				}else {
					for(int i=start;i<=end;i++){
						vehicles[i].meetTrafficLights(trafficLights);
					}
				}
			}
			Thread.sleep(500l);
		}
	}
}
